package uit.edu.vn.Utility;

import java.util.Calendar;

import uit.edu.vn.Activity.LoginActivity;
import uit.edu.vn.Activity.R;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.EditText;
import android.widget.Toast;

public class MyActivity extends Activity{
	private EditText edt_Name;
	static public Context context;
    private static final String STRING_NAME_USERSETTING = "USER REFERENCES";
    private SharedPreferences ShareRef = null;	
    private static String Address = null; //
    
    
    public final String getAddress(){
    	Address = getSetting("address");
    	return Address;
    }
    
    public void saveSetting(String key, String value) {
        ShareRef = getSharedPreferences(STRING_NAME_USERSETTING, Context.MODE_WORLD_WRITEABLE);
        Editor editor = ShareRef.edit();
//        if (value.trim().equals("")) {
//                editor.clear();
//        } else {
                editor.putString(key, value);
//        }
        editor.commit();
    }
    public void logOut() {
        saveSetting("username", "");
        saveSetting("password", "");
        saveSetting("userid", "");
    }

	public String getSetting(String key) {
        ShareRef = getSharedPreferences(STRING_NAME_USERSETTING, Context.MODE_WORLD_WRITEABLE);
        return ShareRef.getString(key, "");
	}
		
	public static String getDateTime(){
		String DATE_FORMAT_NOW = "dd-MM-yyyy";
		String TIME_FORMAT_NOW = "HH-mm-ss";
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = null;
	    sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	    String date = sdf.format(cal.getTime());
	    sdf = new SimpleDateFormat(TIME_FORMAT_NOW);
	    String time = sdf.format(cal.getTime());
		return date+"_"+time;
	}
	
	public static String getSize(String size){
		
		int byteSize = Integer.parseInt(size);
		int result = byteSize/1048576;		
		return String.valueOf(result);
	}
	public static String getFileSize(String size)
	{
		   Double temp=Double.valueOf(size);
           temp=(temp/1024)/1024;
       	//	DecimalFormat df = new DecimalFormat("0.000");
       	//	size= df.format(temp);
           String result;
           result=String.valueOf(temp);
           result=result.substring(0, 4);
		return result;
	}

//-------------------------xu ly show progerss bar----------------------------------------------------------	
    public AlertDialog showNameFileDialog(String title, String msg, String defaultName, OnClickListener yesClick)
    {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.ic_error);
		edt_Name= new EditText(this);
	    edt_Name.setSingleLine();
	    edt_Name.setText(defaultName);
	    builder.setView(edt_Name);
		if(title!=null) builder.setTitle(title);
		if(msg!=null) builder.setMessage(msg);
		return builder.setPositiveButton(getResources().getString(R.string.txt_Yes), yesClick).setNegativeButton(getResources().getString(R.string.txt_No), new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).show();
	}
    public AlertDialog showIpDialog(String title, String msg, OnClickListener yesClick)
    {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.ic_error);
		edt_Name= new EditText(this);
	    edt_Name.setSingleLine();
	  
	    builder.setView(edt_Name);
		if(title!=null) builder.setTitle(title);
		if(msg!=null) builder.setMessage(msg);
		return builder.setPositiveButton(getResources().getString(R.string.txt_Yes), yesClick).setNegativeButton(getResources().getString(R.string.txt_No), new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).show();
	}
    public AlertDialog showAlertDialog(String title, String msg, OnClickListener yesClick)
    {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.ic_info_select);
		if(title!=null) builder.setTitle(title);
		if(msg!=null) builder.setMessage(msg);
		return builder.setPositiveButton(getResources().getString(R.string.txt_Yes), new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		}).show();
    }  

    public AlertDialog showConfirmDialog(String title, String msg, OnClickListener yesClick) 
	{
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(R.drawable.ic_error);
			if(title!=null) builder.setTitle(title);
			if(msg!=null) builder.setMessage(msg);
			return builder.setPositiveButton(getResources().getString(R.string.txt_Yes), yesClick).setNegativeButton(getResources().getString(R.string.txt_No), new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).show();
	}
	public void showToast(int msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
	}

	protected void showToast(String msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
	}
	protected void showToast(int msg, int duration) {
		Toast.makeText(getBaseContext(), msg, duration).show();
	}
				
}
